package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NewAccountServlet extends HttpServlet {
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";
    private static final String NEW_Account_FORM = "/WEB-INF/jsp/account/newAccount.jsp";
    private List<Product> myList;
    private String msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Account account = new Account();
        AccountService accountService = new AccountService();
        CatalogService catalogService = new CatalogService();
        String username = req.getParameter("username");
        String firstName = req.getParameter("account.firstName");
        String lastName = req.getParameter("account.lastName");
        String email = req.getParameter("account.email");
        String phone = req.getParameter("account.phone");
        String address1 = req.getParameter("account.address1");
        String address2 = req.getParameter("account.address2");
        String city = req.getParameter("account.city");
        String state = req.getParameter("account.state");
        String zip = req.getParameter("account.zip");
        String country = req.getParameter("account.country");
        String language = req.getParameter("account.languagePreference");
        String favourite = req.getParameter("account.favouriteCategoryId");
        String password = req.getParameter("password");
        String repeatedPassword = req.getParameter("repeatedPassword");
        String bannerOptionStr = req.getParameter("account.bannerOption");
        String listOptionStr = req.getParameter("account.listOption");
        boolean bannerOption = "on".equalsIgnoreCase(bannerOptionStr);
        boolean listOption = "on".equalsIgnoreCase(listOptionStr);
        String bannerName = "<image src=\"images/banner_" + favourite.toLowerCase() + ".gif\">";


        //更新account对象
        if (username.isEmpty() || password.isEmpty() || repeatedPassword.isEmpty() || firstName.isEmpty() || lastName.isEmpty()
                || email.isEmpty() || phone.isEmpty() || address1.isEmpty() || address2.isEmpty() || city.isEmpty() || state.isEmpty() || zip.isEmpty() || country.isEmpty()) {
            this.msg = "信息不能为空";
            req.setAttribute("msg", this.msg);
            req.getRequestDispatcher(NEW_Account_FORM).forward(req, resp);
        } else {
            if (!password.equals(repeatedPassword)) {
                this.msg = "密码错误";
                req.setAttribute("msg", this.msg);
                req.getRequestDispatcher(NEW_Account_FORM).forward(req, resp);
            } else {
                account.setUsername(username);
                account.setPassword(password);
                account.setFirstName(firstName);
                account.setLastName(lastName);
                account.setEmail(email);
                account.setPhone(phone);
                account.setAddress1(address1);
                account.setAddress2(address2);
                account.setCity(city);
                account.setState(state);
                account.setZip(zip);
                account.setCountry(country);
                account.setLanguagePreference(language);
                account.setFavouriteCategoryId(favourite);
                account.setBannerOption(bannerOption);
                account.setListOption(listOption);
                account.setBannerName(bannerName);


                accountService.insertAccount(account);
                account = accountService.getAccount(account.getUsername());
                this.myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
                //this.authenticated = true;
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
            }
        }
    }
}
